package o;

import android.text.TextUtils;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import o.fus.a;
import o.fuu.e;

public class dmt {
    protected List<String> ˎ = null;
    private ftz ˏ = null;

    public static class b {
        private String ˊ;
        private String ˎ;
        private String ˏ;
        private int ॱ;

        public String ˎ() {
            return this.ˏ;
        }

        public int ॱ() {
            return this.ॱ;
        }

        public String ˋ() {
            return this.ˊ;
        }

        public void ˋ(String str) {
            this.ˊ = str;
        }

        public String ˊ() {
            return this.ˎ;
        }

        public void ˎ(String str) {
            this.ˎ = str;
        }
    }

    private static void ˎ(InputStream inputStream, DataOutputStream dataOutputStream) {
        dnw.ˎ(inputStream);
        dnw.ˎ(dataOutputStream);
    }

    public static Map<String, String> ˊ(String str) {
        Map linkedHashMap = new LinkedHashMap();
        if (!TextUtils.isEmpty(str)) {
            String[] split = str.split(ContainerUtils.FIELD_DELIMITER);
            if (split.length > 0) {
                for (String str2 : split) {
                    if (str2 != null) {
                        String[] split2 = str2.split(ContainerUtils.KEY_VALUE_DELIMITER);
                        if (split2.length > 1) {
                            linkedHashMap.put(split2[0], split2[1]);
                        } else if (split2.length > 0) {
                            linkedHashMap.put(split2[0], "");
                        }
                    }
                }
            }
        }
        return linkedHashMap;
    }

    public void ˏ(List<String> list) {
        this.ˎ = list;
    }

    public b ˎ(String str, String str2, String str3, Map<String, String> map, String str4) {
        Closeable execute;
        Throwable th;
        b bVar = new b();
        if (ॱ(str, str3, map, str4)) {
            return bVar;
        }
        Logger.ˎ("HttpUtil", "start uploadFileStream");
        String str5 = "UTF-8";
        str5 = RequestBody.HEAD_VALUE_CONTENT_TYPE_FORM_DATA;
        Closeable closeable = null;
        dmz o_dmz = new dmz();
        String uuid = UUID.randomUUID().toString();
        try {
            fuv ˊ = dnb.ˊ();
            e eVar = new e();
            eVar.ˏ(fuu.ˋ);
            for (Entry entry : ˊ(str3).entrySet()) {
                String str6 = (String) entry.getKey();
                String[] strArr = new String[]{"Content-Disposition", "form-data; name=\"" + str6 + "\"", "Content-Transfer-Encoding", "8bit"};
                eVar.ˎ(fui.ˋ(strArr), fuw.create(fup.ॱ(RequestBody.DEFAULT_CONTENT_TYPE), dof.ˊ((String) entry.getValue())));
            }
            ˋ((Map) map, "UTF-8", RequestBody.HEAD_VALUE_CONTENT_TYPE_FORM_DATA, eVar);
            a ˋ = new a().ˎ(str).ˋ(eVar.ˎ());
            ˋ.ˋ("Charsert", "UTF-8");
            ˋ.ˋ(HttpContants.KEY_CONTENT_TYPE, "multipart/form-data;boundary=" + uuid);
            if (!dof.ˋ(str2)) {
                ˋ.ˋ("Host", str2);
            }
            ˋ.ˋ("User-Agent", str4);
            execute = ˊ.ˎ(ˋ.ॱ()).execute();
            try {
                ˎ(bVar, "UTF-8", execute, o_dmz);
                ˎ(o_dmz.ॱ(), o_dmz.ˎ());
                dnw.ˎ(execute);
                return bVar;
            } catch (IOException e) {
                try {
                    Logger.e("HttpUtil", "IOException doPostFile error");
                    ˎ(o_dmz.ॱ(), o_dmz.ˎ());
                    dnw.ˎ(execute);
                    return bVar;
                } catch (Throwable th2) {
                    closeable = execute;
                    th = th2;
                    ˎ(o_dmz.ॱ(), o_dmz.ˎ());
                    dnw.ˎ(closeable);
                    throw th;
                }
            }
        } catch (IOException e2) {
            execute = null;
            Logger.e("HttpUtil", "IOException doPostFile error");
            ˎ(o_dmz.ॱ(), o_dmz.ˎ());
            dnw.ˎ(execute);
            return bVar;
        } catch (Throwable th3) {
            th = th3;
            ˎ(o_dmz.ॱ(), o_dmz.ˎ());
            dnw.ˎ(closeable);
            throw th;
        }
    }

    private void ˋ(Map<String, String> map, String str, String str2, e eVar) {
        for (Entry entry : map.entrySet()) {
            String str3 = (String) entry.getKey();
            eVar.ˎ(fui.ˋ(new String[]{"Content-Disposition", "form-data; name=\"" + str3 + "\";filename=\"" + new File((String) entry.getValue()).getName() + "\""}), fuu.create(fup.ॱ(str2 + "; charset=" + str), r3));
        }
    }

    private boolean ॱ(String str, String str2, Map<String, String> map, String str3) {
        if (dof.ॱ(str)) {
            Logger.ˋ("HttpUtil", "url is null, return.");
            return true;
        } else if (dof.ॱ(str2)) {
            Logger.ˋ("HttpUtil", "body is null, return.");
            return true;
        } else if (dof.ॱ(str3)) {
            Logger.ˋ("HttpUtil", "userAgent is null, return.");
            return true;
        } else if (map != null) {
            return false;
        } else {
            Logger.ˋ("HttpUtil", "fileMap is null, return.");
            return true;
        }
    }

    private void ˎ(b bVar, String str, fvb o_fvb, dmz o_dmz) throws IOException {
        bVar.ॱ = o_fvb.ˊ();
        if (o_fvb.ˏ()) {
            o_dmz.ˋ(o_fvb.ᐝ().ˎ());
            o_dmz.ˋ(new InputStreamReader(o_dmz.ˋ(), str));
            o_dmz.ˎ(new BufferedReader(o_dmz.ˊ()));
            bVar.ˏ = doa.ˊ(o_dmz.ˏ(), 102400);
            return;
        }
        bVar.ˋ(o_fvb.ˋ("Maintain-Endtime"));
        bVar.ˎ(o_fvb.ˋ("retry-after"));
        Logger.e("HttpUtil", "bad response:" + bVar.ॱ);
    }

    public b ˋ(String str, String str2, String str3, String str4) throws IOException {
        return ॱ(str, str2, str3, str4, "application/x-gzip");
    }

    public b ॱ(String str, String str2, String str3, String str4, String str5) throws IOException {
        Closeable closeable = null;
        b bVar = new b();
        try {
            fuv ॱ = dnb.ॱ();
            byte[] bytes = str3.getBytes("UTF-8");
            if ("application/x-gzip".equals(str5)) {
                bytes = dms.ˋ(bytes);
            }
            a ˋ = new a().ˎ(str).ˋ(fuw.create(fup.ॱ(str5), bytes));
            ˋ.ˋ(HttpContants.KEY_CONTENT_TYPE, str5);
            if ("application/x-gzip".equals(str5)) {
                ˋ.ˋ("Content-Encoding", "gzip");
            }
            ˋ.ˋ("Connection", "Keep-Alive");
            ˋ.ˋ("User-Agent", str4);
            if (this.ˎ != null) {
                ˋ.ˊ(new ArrayList(this.ˎ));
            }
            if (!dof.ˋ(str2)) {
                ˋ.ˋ("Host", str2);
            }
            ˋ.ˋ(true);
            this.ˏ = ॱ.ˎ(ˋ.ॱ());
            closeable = this.ˏ.execute();
            bVar.ॱ = closeable.ˊ();
            ˏ(closeable, bVar);
        } catch (IllegalStateException e) {
            Logger.e("HttpUtil", "doPost IllegalStateException error:" + e.toString());
        } catch (UnsupportedEncodingException e2) {
            Logger.e("HttpUtil", "doPost UnsupportedEncodingException error:" + e2.toString());
        } catch (ConcurrentModificationException e3) {
            Logger.e("HttpUtil", "doPost ConcurrentModificationException error:");
        } finally {
            dnw.ˎ(closeable);
        }
        return bVar;
    }

    private void ˏ(fvb o_fvb, b bVar) throws IOException {
        if (o_fvb.ˏ()) {
            bVar.ˏ = o_fvb.ᐝ().ʽ();
            return;
        }
        bVar.ˋ(o_fvb.ˋ("Maintain-Endtime"));
        bVar.ˎ(o_fvb.ˋ("retry-after"));
        Logger.e("HttpUtil", "bad response:" + bVar.ॱ);
    }

    public void ˊ() {
        if (this.ˏ != null) {
            try {
                this.ˏ.cancel();
            } catch (Exception e) {
                Logger.e("HttpUtil", "httputil abort exception:" + e.toString());
            }
        }
    }
}
